#!/usr/bin/env python

import os

from google.appengine.ext.webapp import template
from django.utils import simplejson
from google.appengine.ext import webapp
from models.library import Library
from models.setting import Setting

# This controller handles the
# generation of the front page.

class UploadHandler(webapp.RequestHandler):

    def get(self):
        debug = ''

        path = os.path.join(os.path.dirname(__file__), '../views' ,'admin/upload.html')
        self.response.out.write(
        
            # The render method takes the path to a html template,
            # and a dictionary of key/value pairs that will be
            # embedded in the page.
                        
            template.render( path,{
                "debug": debug
        }))


    def post(self):
        
        libs = simplejson.loads(self.request.get('libraries'))
        debug = str(len(libs)) + " libraries:\n"
        count = 0
        for lib in libs:
            count += 1
            if count <= 10:
                debug += lib["title"] + "\n"

            l = Library.create(lib)
            if False:            
                l = Library(
                            title = lib["title"],
                            name = lib["name"],
                            link = lib["link"],
                            have = lib["have"],
                            dont = lib["dont"]
                            )
    
                if lib.has_key("match_isbn"):
                    l.match_isbn = True
                else:
                    l.missing = lib["missing"]

            l.put()
            
        if count > 10:
            debug += "...\n"
    
        # We are using the template module to output the page.
        
        path = os.path.join(os.path.dirname(__file__), '../views' ,'admin/upload.html')
        self.response.out.write(
        
            # The render method takes the path to a html template,
            # and a dictionary of key/value pairs that will be
            # embedded in the page.
            
            template.render( path,{
                "debug": debug
        }))

class MainHandler(webapp.RequestHandler):

    def get(self):
        debug = ''
        
        path = os.path.join(os.path.dirname(__file__), '../views' ,'admin/main.html')
        
        q_problems = Library.all()
        q_problems.filter('has_problems', True)
        
        self.response.out.write(
        
            # The render method takes the path to a html template,
            # and a dictionary of key/value pairs that will be
            # embedded in the page.
                        
            template.render( path,{
                "count" : Library.all().count(10000),
                "count_problems": q_problems.count(10000),
                "debug": debug
        }))

class DeleteHandler(MainHandler):

    def get(self):

        for lib in Library.all():
            lib.delete()

        super(DeleteHandler, self).get()

class ListHandler(webapp.RequestHandler):
    def get(self):
    
        # We are using the template module to output the page.
    
        path = os.path.join(os.path.dirname(__file__), '../views' ,'admin/list.html')
        self.response.out.write(
        
            # The render method takes the path to a html template,
            # and a dictionary of key/value pairs that will be
            # embedded in the page.
            
            template.render( path,{
        }))

class SettingsHandler(webapp.RequestHandler):
    def get(self):
        SETTING_NAMES = ('recaptcha_public',
                        'recaptcha_private',
                        'disabled_contributions'
                        )
        data = {
            "SERVER_NAME" : self.request.environ["SERVER_NAME"]
        }
        
        if self.request.get('save'):
            for name in SETTING_NAMES:
                Setting.save(name, self.request.get(name) or '')
        
        for name in SETTING_NAMES:
            data[name] = Setting.get(name, '')
            
        # We are using the template module to output the page.
    
        path = os.path.join(os.path.dirname(__file__), '../views' ,'admin/settings.html')
        self.response.out.write(
        
            # The render method takes the path to a html template,
            # and a dictionary of key/value pairs that will be
            # embedded in the page.
            
            template.render( path, data))

    def post(self):
        return self.get()

